<!--组件-其他设置-->
<template>
  <div>

    <div class="row" v-if="param && param.isOpen != undefined">
      <div class="row acenter" @click="openClick(!param.isOpen)">
        <text class="font28 main-t1">{{T('其他设置')}}</text>
        <LImage class="arrow-image" :src="param.isOpen ? Config.img('xiala_blue_.png') : Config.img('xiala_blue.png')"></LImage>
      </div>
    </div>

    <div v-if="param && param.isOpen">

      <!--总收益止盈-->
      <VInput class="input" v-if="param.stopProfit != undefined" 
      v-model="param.stopProfit" 
      :isEdit="param.isEdit" 
      :decimal="2" 
      :explain="stopProfitTipe" 
      :title="T('总收益止盈')" 
      :placeholder="T('选填')" 
      :unit="'%'"></VInput>
      
      <!--总亏损止盈-->
      <VInput class="input" v-if="param.stopLoss != undefined" 
      v-model="param.stopLoss" 
      :isEdit="param.isEdit" 
      :decimal="2" 
      :explain="stopLossTipe" 
      :title="T('总亏损止损')" 
      :placeholder="T('选填')" 
      :unit="'%'"></VInput>


      <!--价格上限终止-->
      <VInput class="input" v-if="param.maxPrice != undefined" 
      v-model="param.maxPrice" 
      :isEdit="param.isEdit" 
      :decimal="2" 
      :explain="maxPriceTipe" 
      :title="T('价格上限终止')" 
      :placeholder="T('选填')" 
      :unit="pairMap.baseCoin"></VInput>


      <!--价格下限终止-->
      <VInput class="input" v-if="param.minPrice != undefined" 
      v-model="param.minPrice" 
      :isEdit="param.isEdit" 
      :decimal="2" 
      :explain="minPriceTipe" 
      :title="T('价格下限终止')" 
      :placeholder="T('选填')" 
      :unit="pairMap.baseCoin"></VInput>
      <!--定时自动暂停-->
      <!-- <VInput class="input" v-if="param.pauseTime != undefined" 
      v-model="pauseShowTime" 
      :isEdit="param.isEdit"
      :decimal="0" 
      :integer="6" 
      :explain="pauseTimeTipe" 
      :title="T('定时自动暂停')"  
      :placeholder="T('选填')" 
      :unit="(pauseTimeSelect ? T('分') : null)" 
      @input="pauseOnInput" @focus="pauseOnFocus" @blur="pauseOnBlur"></VInput> -->

      <!--定时自动终止-->
      <VInput class="input" v-if="param.timeStopStamp != undefined" 
      v-model="showTime" 
      :isEdit="param.isEdit"
      :decimal="0" 
      :integer="6" 
      :explain="timeStopStampTipe" 
      :title="T('定时自动终止')"  
      :placeholder="T('选填')" 
      :unit="(timeSelect ? T('分') : null)" 
      @input="onInput" @focus="onFocus" @blur="onBlur"></VInput>

      <!--撤销订单-->
      <VSwitc class="switc" v-if="param.cancelOrder != undefined" 
      v-model="param.cancelOrder" 
      :isEdit="param.isEdit" 
      :explain="cancelOrderTipe" 
      :title="T('终止后撤销未成交的订单')"></VSwitc>

      <!--兑换币种-->
      <VSwitc class="switc" v-if="param.isConvert != undefined" 
      v-model="param.isConvert" 
      :isEdit="param.isEdit" :explain="isConvertTipe" 
      :title="T('终止后自动兑换成投入币种')"></VSwitc>
    </div>
  </div>
</template>

<script>

//组件
import VInput from "@/public/view/VInput.vue";
import VSwitc from "@/public/view/VSwitc.vue";

//功能
const globalEvent = weex.requireModule('globalEvent');
const weexFunction = weex.requireModule('WeexFunction');

import math from "@/_js/math";
import modal from "@/_js/modal";
import Config from "@/_js/config";

import I18N from "@/_i18n/i18n";
var T = I18N.t;

var normParam = { 
  'isOpen':false, //true 显示
  'isEdit':true, //true 可编辑
  'stopProfit':'', //止盈率
  'stopLoss':'', //止损率

  'maxPrice':'', //价格上限终止
  'minPrice':'', //价格下限终止

  'pauseTime':'', //暂停时间戳
  'timeStopStamp':'', //终止时间戳
  'cancelOrder':true, //true 撤销订单
  'isConvert':true, //true 兑换币种
}
export default {

  components: {
    VInput,
    VSwitc,
  },

  model:{
    prop:"param",
    event:"change",
  },
  
  props:{
    param:Object,
    pairMap:Object,
  },

  watch: {
    param: {
      deep: false,
      handler (val, oldVal) {
        this.initParam(val);
        this.initData();
      }
    }
  },
  
  data() {
    return {
      T:T,
      Config:Config,

      timeStamp:'', //当前时间戳

      pauseTimeSelect:false, //true 时间输入框选中
      pauseShowTime:'', //显示的时间
      pauseTimeMinute:'', //终止时间-分
      pauseTimeTipe:T('策略暂停操作优先级高于止盈止损及定时终止，即暂停状态下此类操作不生效。定时终止可能会有几分钟的偏差，触发后自动终止策略，系统根据你的选择决定是否执行撤单兑换操作。'),

      timeSelect:false, //true 时间输入框选中
      showTime:'', //显示的时间
      timeMinute:'', //终止时间-分
      timeStopStampTipe:T('策略暂停操作优先级高于止盈止损及定时终止，即暂停状态下此类操作不生效。定时终止可能会有几分钟的偏差，触发后自动终止策略，系统根据你的选择决定是否执行撤单兑换操作。'),

      stopProfitTipe:T('止盈根据总收益率计算，策略暂停操作优先级高于止盈止损及定时终止，即暂停状态下此类操作不生效。触发止盈后自动终止策略，系统根据你的选择决定是否执行撤单兑换操作。'),
      stopLossTipe:T('止损根据总收益率计算，策略暂停操作优先级高于止盈止损及定时终止，即暂停状态下此类操作不生效。触发止损后自动终止策略，系统根据你的选择决定是否执行撤单兑换操作。'),
      
      maxPriceTipe:T('行情价超过该价格终止策略'),
      minPriceTipe:T('行情价低于该价格终止策略'),

      cancelOrderTipe:T('开启此选项，策略终止时将自动撤销所有未成交的委托单。'),
      isConvertTipe:T('开启此选项，策略终止时将自动将资产以市价进行委托兑换成投入币种；如果未开启终止后撤销未成交委托单功能，将只兑换未冻结部分的资产。深度不足的币种可能面临风险，具体兑换结果以交易所为准。'),
    };
  },

  mounted(){

    this.initParam(this.param);
    this.initData();
  },

  methods:{

    //初始化参数
    initParam(param){

      if (param == undefined || JSON.stringify(param) == '{}') {
        this.param = JSON.parse(JSON.stringify(normParam));
        this.$emit("change", this.param); 
      }
    },

    //初始化数据
    initData(){
  
      //1、还原输入"终止时间-分"
      this.timeMinute = '';
      this.pauseTimeMinute = '';

      //2、获取当前时间戳，去除秒
      var timeStamp = (new Date().getTime()) + '';
      timeStamp = math.chu(timeStamp, '60000', 0, 2);
      this.timeStamp = math.cheng(timeStamp, '60000', 0, 2);

      //3、运算"终止时间-分"
      if (this.param.pauseTime != undefined && this.param.pauseTime.length) {

        var time = math.jian(this.param.pauseTime, this.timeStamp);
        var pauseTimeMinute = math.chu(time, '60000', 0, 2);
        if (math.getNumber(timeMinute) < 0) {
          this.timeStamp = this.param.pauseTime;
          this.pauseTimeMinute = '0';
        } else {
          this.pauseTimeMinute = pauseTimeMinute;
        }
      }
      if (this.param.timeStopStamp != undefined && this.param.timeStopStamp.length) {

        var time = math.jian(this.param.timeStopStamp, this.timeStamp);
        var timeMinute = math.chu(time, '60000', 0, 2);
        if (math.getNumber(timeMinute) < 0) {
          this.timeStamp = this.param.timeStopStamp;
          this.timeMinute = '0';
        } else {
          this.timeMinute = timeMinute;
        }
      }

      //4、设置显示时间-终止时间
      this.pauseShowTime = this.getTimeStop(this.pauseTimeMinute);
      this.showTime = this.getTimeStop(this.timeMinute);
    },

    //检查
    checkError(){

      if (this.param.stopProf != undefined && this.param.stopProfit.length > 0 && math.getNumber(this.param.stopProfit) == 0) {
        modal.toast(T("总收益止盈不可为0"));
        return true;
      }
      if (this.param.stopLoss != undefined && this.param.stopLoss.length > 0 && math.getNumber(this.param.stopLoss) == 0) {
        modal.toast(T("总亏损止损不可为0"));
        return true;
      }

      if (this.param.pauseTime != undefined) {

        var stamp1 = this.getTimeStopStamp(this.pauseTimeMinute);
        stamp1 = math.getNumber(stamp1);

        var stamp2 = (new Date().getTime())+ '';
        stamp2 = math.getNumber(stamp2);

        if (this.pauseTimeMinute.length > 0 && stamp2 >= stamp1) {
          modal.toast(T("定时自动暂停时间已过期"));
          return true;
        }
      }

      if (this.param.timeStopStamp != undefined) {

        var stamp1 = this.getTimeStopStamp(this.timeMinute);
        stamp1 = math.getNumber(stamp1);

        var stamp2 = (new Date().getTime())+ '';
        stamp2 = math.getNumber(stamp2);

        if (this.timeMinute.length > 0 && stamp2 >= stamp1) {
          modal.toast(T("定时自动终止时间已过期"));
          return true;
        }
      }
      return false;
    },
    
    //获取参数
    getParam(){

      var param = {'cancelOrder':this.param.cancelOrder, //撤销订单
                   'isConvert':this.param.isConvert, //兑换币种
                  };
      if (this.param.stopProfit != undefined) {
        param['stopProfit'] = this.param.stopProfit.length ? math.chu(this.param.stopProfit, '100') : '';
      }
      if (this.param.stopLoss != undefined) {
        param['stopLoss'] = this.param.stopLoss.length ? math.chu(this.param.stopLoss, '-100') : '';
      }

      if (this.param.maxPrice != undefined) {
        param['maxPrice'] = this.param.maxPrice;
      }
      if (this.param.minPrice != undefined) {
        param['minPrice'] = this.param.minPrice;
      }

      if (this.param.pauseTime != undefined) { 
        param['pauseTime'] = this.getTimeStopStamp(this.pauseTimeMinute);
      }
      if (this.param.timeStopStamp != undefined) { 
        param['stopTime'] = this.getTimeStopStamp(this.timeMinute);
      }
      return param;
    },
    
    /********************************** Get方法*/
    //获取终止时间戳
    getTimeStopStamp(timeMinute) {
      if (timeMinute.length == 0) {
        return '';
      }
      var time = math.cheng(timeMinute, '60000');
      time = math.jia(this.timeStamp, time);
      return time;
    },

    //获取终止时间-年/月/日 时:分
    getTimeStop(timeMinute) {

      if (timeMinute.length == 0) {
        return '';
      }

      var time = this.getTimeStopStamp(timeMinute);
      time = math.getNumber(time);
      time = this.getTime(time);  
      return time;
    },

    //获取时间-年/月/日 时:分
    getTime(timestamp){

      var time = new Date(Number(timestamp));

      var year = time.getFullYear()

      var month = time.getMonth()+1
      month = month < 10 ? "0"+month : month;

      var day = time.getDate();
      day = day < 10 ? "0"+day : day;

      var hour = time.getHours();
      hour = hour < 10 ? "0"+hour : hour;

      var minute = time.getMinutes();
      minute = minute < 10 ? "0"+minute : minute;

      return year+"/"+month+"/"+day+' '+hour+':'+minute;
    },

    /**********************************************输入框回调*/
    //输入框-选中
    pauseOnFocus(e) {
      this.pauseTimeSelect = true;
      this.pauseShowTime = this.pauseTimeMinute;
    },

    //输入框-取消选中
    pauseOnBlur(e) {
      this.pauseTimeSelect = false;
      this.pauseShowTime = this.getTimeStop(this.pauseTimeMinute);
    },

    ///输入内容改变
    pauseOnInput(e){
      this.pauseTimeMinute = e.value;
      this.param.pauseTime = this.getTimeStopStamp(this.pauseTimeMinute);
    },
    
    //输入框-选中
    onFocus(e) {
      this.timeSelect = true;
      this.showTime = this.timeMinute;
    },

    //输入框-取消选中
    onBlur(e) {
      this.timeSelect = false;
      this.showTime = this.getTimeStop(this.timeMinute);
    },

    ///输入内容改变
    onInput(e){
      this.timeMinute = e.value;
      this.param.timeStopStamp = this.getTimeStopStamp(this.timeMinute);
    },

    /********************************** 点击触发*/
    //展开点击
    openClick(isOpen){
      this.param.isOpen = isOpen;
      isOpen && this.$emit("open");
    },
  }
}
</script>


<style src="@/_css/style.css"></style>
<style scoped>
.arrow-image {
  width: 20px;
  height: 20px;
  margin-left: 20px;
  margin-right: 20px;
}

.input {
  margin-top: 40px;
}

.switc {
  margin-top: 40px;
}
</style>
